﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Xml;
using System.IO;
using System.Web;
using System.Reflection;
using System.Diagnostics;
using System.Data.SqlClient;
using Microsoft.Win32;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.ApplicationPages.WebControls;
using Microsoft.SharePoint.WebControls;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace SPSFRv2
{
    class Program
    {
        public string DSN, confgDbServerName, confgDbName, BuildVersion, systemAccount, adminURL, adminDbName, adminDbServerName, exceptionInformation;
        public int count, Servernum, totalContentDBCount, WebAppnum, ContentDBcount, serviceAppPoolCount, FeatureCount, sFeatureCount, wFeatureCount, solutionCount;
        public string[] Servers, ServersId, serviceApps, serviceAppPools, serviceAppProxies, serviceAppProxyGroup;
        public string[,] ServicesOnServers, WebAppDetails, WebAppExtended, WebAppInternalAAMURL, WebAppPublicAAM, WebAppAuthProviders;
        public string[,] ContentDBs, WebAppDbID, ContentDBSitesNum, solutionProps, ContentDBProps, FarmFeatures, SiteFeatures, WebFeatures;
        public string[, ,] serverProducts;
        StreamWriter sw = new StreamWriter(".\\2010SPSFR.html");
        Program() { }

        public bool o14farmConfig()
        {
            RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Shared Tools\\Web Server Extensions\\14.0\\Secure\\configdb");
            if (masterKey == null)
            {
                Console.WriteLine("You will need to run this program on a server where SharePoint is installed.");
                int exitCode = 0;
                Environment.Exit(exitCode);
            }
            DSN = (string)masterKey.GetValue("DSN");
            if (DSN == null)
            {
                Console.WriteLine("You will need to run this program on a server where SharePoint is installed.");
                int exitCode = 0;
                Environment.Exit(exitCode);
            }

            confgDbServerName = DSN.Split(new Char[] { '=', ';' })[1];
            confgDbName = DSN.Split(new Char[] { '=', ';' })[3];
            masterKey.Close();
            try
            {
                SPFarm mySPFarm = SPWebService.ContentService.Farm;
                BuildVersion = mySPFarm.BuildVersion.ToString();
                systemAccount = mySPFarm.TimerService.ProcessIdentity.Username.ToString();
                SPServerCollection mySPServerCollection = mySPFarm.Servers;
                SPWebApplicationCollection mySPAdminWebAppCollection = SPWebService.AdministrationService.WebApplications;
                SPTimerService spts = mySPFarm.TimerService;

                if (mySPAdminWebAppCollection != null)
                {
                    foreach (SPAdministrationWebApplication mySPAdminWebApp in mySPAdminWebAppCollection)
                    {
                        if (mySPAdminWebApp.IsAdministrationWebApplication)
                        {
                            foreach (SPAlternateUrl mySPAlternateUrl in mySPAdminWebApp.AlternateUrls)
                            {
                                switch (mySPAlternateUrl.UrlZone.ToString().Trim())
                                {
                                    case "Default":
                                        adminURL = mySPAlternateUrl.IncomingUrl.ToString();
                                        break;
                                }
                            }
                            SPContentDatabaseCollection mySPContentDBCollection = mySPAdminWebApp.ContentDatabases;
                            foreach (SPContentDatabase mySPContentDB in mySPContentDBCollection)
                            {
                                adminDbName = mySPContentDB.Name.ToString();
                                adminDbServerName = mySPContentDB.Server.ToString();
                            }

                        }
                    }
                }
                masterKey.Close();
                return true;
            }
            catch (Exception ex)
            {
                exceptionInformation = ex.Message;
                return false;
                //Environment.Exit(0);
            }

        }

        public bool o14enumServers()
        {
            try
            {
                SPFarm mySPFarm = SPWebService.ContentService.Farm;
                SPServerCollection mySPServerCollection = mySPFarm.Servers;

                //To get the number of servers in farm
                Servernum = mySPServerCollection.Count;
                ServicesOnServers = new string[Servernum, 50];
                Servers = new string[Servernum];
                ServersId = new string[Servernum];
                int count, ServicesCount, count2;
                count = 0;

                foreach (SPServer ServerInstance in mySPServerCollection)
                {
                    count2 = 0;
                    Servers[count] = ServerInstance.Address;
                    ServersId[count] = ServerInstance.Id.ToString();
                    ServicesCount = ServerInstance.ServiceInstances.Count;
                    ServicesOnServers[count, 49] = ServerInstance.ServiceInstances.Count.ToString();
                    //object versions = ServerInstance.GetType().GetProperty("Versions", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(ServerInstance, null);
                    //PropertyInfo pInfo = versions.GetType().GetProperty("Values"); ;
                    //IEnumerable ValueCollection = (IEnumerable) pInfo.GetValue(versions, null);
                    //Console.WriteLine(ValueCollection.ToString());
                    ////PropertyInfo[] pInfo2 = ValueCollection.;
                    //foreach (KeyValuePair<Guid, Version> vE in ValueCollection.GetType())
                    //    Console.WriteLine(vE.Key.ToString() + "\t" + vE.Value.ToString());

                    //var value =  versions.GetType().GetProperty("").GetValue(ValueCollection, new object[] {0});
                    //PropertyInfo[] pInfo2 = ValueCollection.GetType().GetProperties(); 
                    //var value = ValueCollection.GetType().GetProperty("Build");
                    //Object _Build = value.GetValue(ValueCollection, new object[] {0});
                    //var value =  versions.GetType().GetProperty("Values").GetValue(versions, new object[] {1});

                    foreach (SPServiceInstance serviceInstance in ServerInstance.ServiceInstances)
                    {
                        if (serviceInstance.Hidden == false && serviceInstance.Status.ToString().Trim().ToLower() == "online")
                        {
                            switch (serviceInstance.Service.TypeName)
                            {
                                case "Microsoft.Office.Server.Search.Administration.SearchQueryAndSiteSettingsService":
                                    ServicesOnServers[count, count2] = "Search Query and Site Settings Service";
                                    break;

                                case "Microsoft.Office.Server.ApplicationRegistry.SharedService.ApplicationRegistryService":
                                    ServicesOnServers[count, count2] = "Application Registry Service";
                                    break;

                                default:
                                    ServicesOnServers[count, count2] = serviceInstance.Service.TypeName;
                                    break;
                            }
                            count2++;
                        }
                    }

                    count = count + 1;
                }
                return true;
            }
            catch (Exception ex)
            {
                exceptionInformation = ex.Message;
                return false;
            }
        }

        public bool o14enumProdVersions()
        {
            try
            {
                serverProducts = new string[Servernum, 15, 60];
                count = Servernum - 1;
                int count2, count3;

                SPProductVersions versions = SPProductVersions.GetProductVersions();
                List<SPServerProductInfo> infos = versions.ServerInformation;

                foreach (SPServerProductInfo prodInfo in infos)
                {
                    count2 = 0;
                    count3 = 0;
                    List<string> products = prodInfo.Products;
                    products.Sort();
                    serverProducts[count, count2, count3] = prodInfo.ServerName;
                    //Console.WriteLine(prodInfo.ServerName);
                    //Console.WriteLine();
                    foreach (string str in products)
                    {
                        count2++;
                        serverProducts[count, count2, count3] = str;
                        SPProduct singleProductInfo = prodInfo.GetSingleProductInfo(str);
                        List<string> patchableUnitDisplayNames = singleProductInfo.PatchableUnitDisplayNames;
                        patchableUnitDisplayNames.Sort();
                        foreach (string str2 in patchableUnitDisplayNames)
                        {
                            List<SPPatchableUnitInfo> patchableUnitInfoByDisplayName = singleProductInfo.GetPatchableUnitInfoByDisplayName(str2);
                            foreach (SPPatchableUnitInfo info in patchableUnitInfoByDisplayName)
                            {
                                count3++;
                                string version = SPHttpUtility.HtmlEncode(info.BaseVersionOnServer(prodInfo.ServerId).ToString());
                                //Console.WriteLine(info.DisplayName + ", " + version);
                                serverProducts[count, count2, count3] = info.DisplayName + ":" + version;
                            }
                            serverProducts[count, count2, 59] = count3.ToString();
                        }
                        serverProducts[count, 14, 59] = count2.ToString();
                        count3 = 0;
                    }
                    count--;
                }
                return true;
            }
            catch (Exception ex)
            {
                exceptionInformation = ex.Message;
                return false;
            }
        }

        public bool o14enumFeatures()
        {
            try
            {
                SPFarm mySPFarm = SPWebService.ContentService.Farm;
                FeatureCount = 0;
                int FeatureCount2 = 0;
                PropertyInfo pi;

                //to retrieve the number of features deployed in farm
                foreach (SPFeatureDefinition FeatureDefinition in mySPFarm.FeatureDefinitions)
                {
                    if (FeatureDefinition.Hidden != true && FeatureDefinition.Scope.ToString() == "Farm")
                        FeatureCount++;
                }
                FarmFeatures = new string[FeatureCount, 4];

                //to retrieve the properties
                foreach (SPFeatureDefinition FeatureDefinition in mySPFarm.FeatureDefinitions)
                {
                    if (FeatureDefinition.Hidden != true && FeatureDefinition.Scope.ToString() == "Farm")
                    {
                        switch (FeatureDefinition.DisplayName)
                        {
                            case "AccSrvApplication":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Access Services Farm Feature";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "GlobalWebParts":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Global Web Parts";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "VisioServer":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Visio Web Access";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "SpellChecking":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Spell Checking";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "SocialRibbonControl":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Social Tags and Note Board Ribbon Controls";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "VisioProcessRepositoryFeatureStapling":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Visio Process Repository";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "DownloadFromOfficeDotCom":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Office.com Entry Points from SharePoint";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "ExcelServerWebPartStapler":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Excel Services Application Web Part Farm Feature";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "DataConnectionLibraryStapling":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Data Connection Library";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "FastFarmFeatureActivation":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "FAST Search Server 2010 for SharePoint Master Job Provisioning";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "ExcelServer":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Excel Services Application View Farm Feature";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "ObaStaple":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "\"Connect to Office\" Ribbon Controls";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            case "TemplateDiscovery":
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = "Offline Synchronization for External Lists";
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                            default:
                                FarmFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                                FarmFeatures[FeatureCount2, 1] = FeatureDefinition.DisplayName;
                                FarmFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                                pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                                FarmFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                                break;
                        }
                        FeatureCount2++;
                    }
                }

                sFeatureCount = 0;
                FeatureCount2 = 0;
                foreach (SPFeatureDefinition FeatureDefinition in mySPFarm.FeatureDefinitions)
                {
                    if (FeatureDefinition.Hidden != true && FeatureDefinition.Scope.ToString() == "Site")
                        sFeatureCount++;
                }
                SiteFeatures = new string[sFeatureCount, 4];
                foreach (SPFeatureDefinition FeatureDefinition in mySPFarm.FeatureDefinitions)
                {
                    if (FeatureDefinition.Hidden != true && FeatureDefinition.Scope.ToString() == "Site")
                    {
                        SiteFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                        SiteFeatures[FeatureCount2, 1] = FeatureDefinition.DisplayName;
                        SiteFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                        pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                        SiteFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                        FeatureCount2++;
                    }
                }

                wFeatureCount = 0;
                FeatureCount2 = 0;
                foreach (SPFeatureDefinition FeatureDefinition in mySPFarm.FeatureDefinitions)
                {
                    if (FeatureDefinition.Hidden != true && FeatureDefinition.Scope.ToString() == "Web")
                        wFeatureCount++;
                }
                WebFeatures = new string[wFeatureCount, 4];
                foreach (SPFeatureDefinition FeatureDefinition in mySPFarm.FeatureDefinitions)
                {
                    if (FeatureDefinition.Hidden != true && FeatureDefinition.Scope.ToString() == "Web")
                    {
                        WebFeatures[FeatureCount2, 0] = FeatureDefinition.Id.ToString();
                        WebFeatures[FeatureCount2, 1] = FeatureDefinition.DisplayName;
                        WebFeatures[FeatureCount2, 2] = FeatureDefinition.SolutionId.ToString();
                        pi = FeatureDefinition.GetType().GetProperty("HasActivations", BindingFlags.NonPublic | BindingFlags.Instance);
                        WebFeatures[FeatureCount2, 3] = pi.GetValue(FeatureDefinition, null).ToString();
                        FeatureCount2++;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                exceptionInformation = ex.Message;
                return false;
            }

        }

        public bool o14enumSvcApps()
        {
            try
            {
                string[] delimiterChars = new string[] { "TypeName" };
                string[] delimiterChars2 = new string[] { "\r\n\r\n" };
                string[] delimiterChars3 = new string[] { "FriendlyName" };
                RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
                PSSnapInException snapInException = null;
                PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.SharePoint.PowerShell", out snapInException);
                if (snapInException != null)
                {
                    exceptionInformation = snapInException.Message.ToString();
                    return false;
                }
                Runspace runspace = RunspaceFactory.CreateRunspace(rsConfig);
                runspace.Open();
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript("$FormatEnumerationLimit = 25");
                //Run Get-SPServiceApplication
                pipeline.Commands.AddScript("Get-SPServiceApplication | fl");
                pipeline.Commands.Add("Out-String");
                Collection<PSObject> results = pipeline.Invoke();
                if (results.Count == 0)
                {
                    exceptionInformation = "Running a Get-SPServiceApplication did not yield any results.";
                    return false;
                }
                serviceApps = results[0].BaseObject.ToString().Split(delimiterChars, StringSplitOptions.None);

                //Run Get-SPServiceApplicationPool
                pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript("$FormatEnumerationLimit = 25");
                pipeline.Commands.AddScript("Get-SPServiceApplicationPool | select Id, Name, ProcessAccountName | fl");
                pipeline.Commands.Add("Out-String");
                results = pipeline.Invoke();
                serviceAppPoolCount = results.Count;
                serviceAppPools = results[0].BaseObject.ToString().Split(delimiterChars2, StringSplitOptions.None);

                //Run Get-SPServiceApplicationProxy
                pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript("$FormatEnumerationLimit = 25");
                pipeline.Commands.AddScript("Get-SPServiceApplicationProxy | fl");
                pipeline.Commands.Add("Out-String");
                results = pipeline.Invoke();
                serviceAppPoolCount = results.Count;
                serviceAppProxies = results[0].BaseObject.ToString().Split(delimiterChars, StringSplitOptions.None);

                //Run Get-SPServiceApplicationProxyGroup
                pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript("$FormatEnumerationLimit = 25");
                pipeline.Commands.AddScript("Get-SPServiceApplicationProxyGroup | select id,friendlyname, proxies, defaultProxies | fl");
                pipeline.Commands.Add("Out-String");
                results = pipeline.Invoke();
                serviceAppPoolCount = results.Count;
                serviceAppProxyGroup = results[0].BaseObject.ToString().Split(delimiterChars2, StringSplitOptions.None);

                runspace.Close();
                return true;
            }
            catch (Exception ex)
            {
                exceptionInformation = ex.Message;
                return false;
            }
        }

        public bool o14enumWebApps()
        {
            try
            {
                totalContentDBCount = 0;
                SPFarm mySPFarm = SPWebService.ContentService.Farm;
                SPServerCollection mySPServerCollection = mySPFarm.Servers;
                SPWebApplicationCollection mySPWebAppCollection = SPWebService.ContentService.WebApplications;
                WebAppnum = mySPWebAppCollection.Count;
                WebAppDetails = new string[WebAppnum, 10];
                WebAppExtended = new string[WebAppnum, 6];
                WebAppInternalAAMURL = new string[WebAppnum, 6];
                WebAppPublicAAM = new string[WebAppnum, 6];
                WebAppAuthProviders = new string[WebAppnum, 6];
                count = 0;
                ContentDBs = new string[WebAppnum, 141];
                WebAppDbID = new string[WebAppnum, 141];
                ContentDBSitesNum = new string[WebAppnum, 141];
                int mySiteCollectionNum;
                System.Web.Configuration.AuthenticationMode authMode;

                if (mySPWebAppCollection != null)
                {
                    foreach (SPWebApplication mySPWebApp in mySPWebAppCollection)
                    {
                        mySiteCollectionNum = 0;
                        WebAppDetails[count, 0] = count.ToString();
                        WebAppExtended[count, 0] = count.ToString();
                        WebAppInternalAAMURL[count, 0] = count.ToString();
                        WebAppPublicAAM[count, 0] = count.ToString();

                        WebAppDetails[count, 2] = mySPWebApp.Name;
                        WebAppDetails[count, 6] = mySPWebApp.ApplicationPool.Name;
                        WebAppDetails[count, 7] = mySPWebApp.ApplicationPool.Username;
                        WebAppDetails[count, 3] = mySPWebApp.ContentDatabases.Count.ToString();
                        if (mySPWebApp.ServiceApplicationProxyGroup.Name == "")
                            WebAppDetails[count, 9] = "[default]";
                        else
                            WebAppDetails[count, 9] = mySPWebApp.ServiceApplicationProxyGroup.Name;
                        ContentDBcount = mySPWebApp.ContentDatabases.Count;
                        totalContentDBCount = totalContentDBCount + mySPWebApp.ContentDatabases.Count;

                        if (mySPWebApp.IisSettings.ContainsKey(SPUrlZone.Default))
                        {
                            authMode = mySPWebApp.IisSettings[SPUrlZone.Default].AuthenticationMode;
                            if (authMode.ToString().ToLower().Trim() == "none")
                                WebAppAuthProviders[count, 1] = "ADFS / WebSSO";
                            else
                                WebAppAuthProviders[count, 1] = authMode.ToString().Trim();

                            if (mySPWebApp.IisSettings[SPUrlZone.Default].DisableKerberos == true && WebAppAuthProviders[count, 1] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Default].UseWindowsIntegratedAuthentication && !mySPWebApp.IisSettings[SPUrlZone.Default].UseBasicAuthentication)
                                WebAppAuthProviders[count, 1] = WebAppAuthProviders[count, 1] + " (NTLM)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Default].DisableKerberos == true && WebAppAuthProviders[count, 1] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Default].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Default].UseBasicAuthentication)
                                WebAppAuthProviders[count, 1] = WebAppAuthProviders[count, 1] + " (NTLM + Basic)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Default].DisableKerberos == false && WebAppAuthProviders[count, 1] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Default].UseWindowsIntegratedAuthentication && !mySPWebApp.IisSettings[SPUrlZone.Default].UseBasicAuthentication)
                                WebAppAuthProviders[count, 1] = WebAppAuthProviders[count, 1] + " (Kerberos)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Default].DisableKerberos == false && WebAppAuthProviders[count, 1] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Default].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Default].UseBasicAuthentication)
                                WebAppAuthProviders[count, 1] = WebAppAuthProviders[count, 1] + " (Kerberos + Basic)";

                            if (WebAppAuthProviders[count, 1] == "Windows" && !mySPWebApp.IisSettings[SPUrlZone.Default].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Default].UseBasicAuthentication)
                                WebAppAuthProviders[count, 1] = WebAppAuthProviders[count, 1] + " (Basic)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Default].AllowAnonymous)
                                WebAppAuthProviders[count, 1] = WebAppAuthProviders[count, 1] + "| true";
                            else
                                WebAppAuthProviders[count, 1] = WebAppAuthProviders[count, 1] + "| false";

                            WebAppAuthProviders[count, 1] = WebAppAuthProviders[count, 1] + "| " + mySPWebApp.IisSettings[SPUrlZone.Default].Path.FullName;

                            WebAppAuthProviders[count, 1] = WebAppAuthProviders[count, 1] + "| " + mySPWebApp.IisSettings[SPUrlZone.Default].UseClaimsAuthentication.ToString();

                        }

                        if (mySPWebApp.IisSettings.ContainsKey(SPUrlZone.Intranet))
                        {
                            WebAppExtended[count, 2] = "true";
                            authMode = mySPWebApp.IisSettings[SPUrlZone.Intranet].AuthenticationMode;
                            if (authMode.ToString().ToLower().Trim() == "none")
                                WebAppAuthProviders[count, 2] = "ADFS / WebSSO";
                            else
                                WebAppAuthProviders[count, 2] = authMode.ToString().Trim();

                            if (mySPWebApp.IisSettings[SPUrlZone.Intranet].DisableKerberos == true && WebAppAuthProviders[count, 2] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Intranet].UseWindowsIntegratedAuthentication && !mySPWebApp.IisSettings[SPUrlZone.Intranet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 2] = WebAppAuthProviders[count, 2] + " (NTLM)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Intranet].DisableKerberos == true && WebAppAuthProviders[count, 2] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Intranet].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Intranet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 2] = WebAppAuthProviders[count, 2] + " (NTLM + Basic)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Intranet].DisableKerberos == false && WebAppAuthProviders[count, 2] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Intranet].UseWindowsIntegratedAuthentication && !mySPWebApp.IisSettings[SPUrlZone.Intranet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 2] = WebAppAuthProviders[count, 2] + " (Kerberos)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Intranet].DisableKerberos == false && WebAppAuthProviders[count, 2] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Intranet].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Intranet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 2] = WebAppAuthProviders[count, 2] + " (Kerberos + Basic)";

                            if (WebAppAuthProviders[count, 2] == "Windows" && !mySPWebApp.IisSettings[SPUrlZone.Intranet].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Intranet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 2] = WebAppAuthProviders[count, 2] + " (Basic)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Intranet].AllowAnonymous)
                                WebAppAuthProviders[count, 2] = WebAppAuthProviders[count, 2] + "| true";
                            else
                                WebAppAuthProviders[count, 2] = WebAppAuthProviders[count, 2] + "| false";

                            WebAppAuthProviders[count, 2] = WebAppAuthProviders[count, 2] + "| " + mySPWebApp.IisSettings[SPUrlZone.Intranet].Path.FullName;

                            WebAppAuthProviders[count, 2] = WebAppAuthProviders[count, 2] + "| " + mySPWebApp.IisSettings[SPUrlZone.Intranet].UseClaimsAuthentication.ToString();

                        }


                        if (mySPWebApp.IisSettings.ContainsKey(SPUrlZone.Internet))
                        {
                            WebAppExtended[count, 3] = "true";
                            authMode = mySPWebApp.IisSettings[SPUrlZone.Internet].AuthenticationMode;
                            if (authMode.ToString().ToLower().Trim() == "none")
                                WebAppAuthProviders[count, 3] = "ADFS / WebSSO";
                            else
                                WebAppAuthProviders[count, 3] = authMode.ToString().Trim();

                            if (mySPWebApp.IisSettings[SPUrlZone.Internet].DisableKerberos == true && WebAppAuthProviders[count, 3] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Internet].UseWindowsIntegratedAuthentication && !mySPWebApp.IisSettings[SPUrlZone.Internet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 3] = WebAppAuthProviders[count, 3] + " (NTLM)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Internet].DisableKerberos == true && WebAppAuthProviders[count, 3] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Internet].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Internet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 3] = WebAppAuthProviders[count, 3] + " (NTLM + Basic)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Internet].DisableKerberos == false && WebAppAuthProviders[count, 3] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Internet].UseWindowsIntegratedAuthentication && !mySPWebApp.IisSettings[SPUrlZone.Internet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 3] = WebAppAuthProviders[count, 3] + " (Kerberos)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Internet].DisableKerberos == false && WebAppAuthProviders[count, 3] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Internet].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Internet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 3] = WebAppAuthProviders[count, 3] + " (Kerberos + Basic)";

                            if (WebAppAuthProviders[count, 3] == "Windows" && !mySPWebApp.IisSettings[SPUrlZone.Internet].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Internet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 3] = WebAppAuthProviders[count, 3] + " (Basic)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Internet].AllowAnonymous)
                                WebAppAuthProviders[count, 3] = WebAppAuthProviders[count, 3] + "| true";
                            else
                                WebAppAuthProviders[count, 3] = WebAppAuthProviders[count, 3] + "| false";

                            WebAppAuthProviders[count, 3] = WebAppAuthProviders[count, 3] + "| " + mySPWebApp.IisSettings[SPUrlZone.Internet].Path.FullName;

                            WebAppAuthProviders[count, 3] = WebAppAuthProviders[count, 3] + "| " + mySPWebApp.IisSettings[SPUrlZone.Internet].UseClaimsAuthentication.ToString();

                        }

                        if (mySPWebApp.IisSettings.ContainsKey(SPUrlZone.Custom))
                        {
                            WebAppExtended[count, 4] = "true";
                            authMode = mySPWebApp.IisSettings[SPUrlZone.Custom].AuthenticationMode;
                            if (authMode.ToString().ToLower().Trim() == "none")
                                WebAppAuthProviders[count, 4] = "ADFS / WebSSO";
                            else
                                WebAppAuthProviders[count, 4] = authMode.ToString().Trim();

                            if (mySPWebApp.IisSettings[SPUrlZone.Custom].DisableKerberos == true && WebAppAuthProviders[count, 4] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Custom].UseWindowsIntegratedAuthentication && !mySPWebApp.IisSettings[SPUrlZone.Custom].UseBasicAuthentication)
                                WebAppAuthProviders[count, 4] = WebAppAuthProviders[count, 4] + " (NTLM)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Custom].DisableKerberos == true && WebAppAuthProviders[count, 4] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Custom].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Custom].UseBasicAuthentication)
                                WebAppAuthProviders[count, 4] = WebAppAuthProviders[count, 4] + " (NTLM + Basic)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Custom].DisableKerberos == false && WebAppAuthProviders[count, 4] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Custom].UseWindowsIntegratedAuthentication && !mySPWebApp.IisSettings[SPUrlZone.Custom].UseBasicAuthentication)
                                WebAppAuthProviders[count, 4] = WebAppAuthProviders[count, 4] + " (Kerberos)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Custom].DisableKerberos == false && WebAppAuthProviders[count, 4] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Custom].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Custom].UseBasicAuthentication)
                                WebAppAuthProviders[count, 4] = WebAppAuthProviders[count, 4] + " (Negotiate + Basic)";

                            if (WebAppAuthProviders[count, 4] == "Windows" && !mySPWebApp.IisSettings[SPUrlZone.Custom].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Custom].UseBasicAuthentication)
                                WebAppAuthProviders[count, 4] = WebAppAuthProviders[count, 4] + " (Basic)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Custom].AllowAnonymous)
                                WebAppAuthProviders[count, 4] = WebAppAuthProviders[count, 4] + "| true";
                            else
                                WebAppAuthProviders[count, 4] = WebAppAuthProviders[count, 4] + "| false";

                            WebAppAuthProviders[count, 4] = WebAppAuthProviders[count, 4] + "| " + mySPWebApp.IisSettings[SPUrlZone.Custom].Path.FullName;

                            WebAppAuthProviders[count, 4] = WebAppAuthProviders[count, 4] + "| " + mySPWebApp.IisSettings[SPUrlZone.Custom].UseClaimsAuthentication.ToString();
                        }

                        if (mySPWebApp.IisSettings.ContainsKey(SPUrlZone.Extranet))
                        {
                            authMode = mySPWebApp.IisSettings[SPUrlZone.Extranet].AuthenticationMode;
                            if (authMode.ToString().ToLower().Trim() == "none")
                                WebAppAuthProviders[count, 5] = "ADFS / WebSSO";
                            else
                                WebAppAuthProviders[count, 5] = authMode.ToString().Trim();
                            WebAppExtended[count, 5] = "true";

                            if (mySPWebApp.IisSettings[SPUrlZone.Extranet].DisableKerberos == true && WebAppAuthProviders[count, 5] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Extranet].UseWindowsIntegratedAuthentication && !mySPWebApp.IisSettings[SPUrlZone.Extranet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 5] = WebAppAuthProviders[count, 5] + " (NTLM)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Extranet].DisableKerberos == true && WebAppAuthProviders[count, 5] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Extranet].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Extranet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 5] = WebAppAuthProviders[count, 5] + " (NTLM + Basic)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Extranet].DisableKerberos == false && WebAppAuthProviders[count, 5] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Extranet].UseWindowsIntegratedAuthentication && !mySPWebApp.IisSettings[SPUrlZone.Extranet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 5] = WebAppAuthProviders[count, 5] + " (Kerberos)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Extranet].DisableKerberos == false && WebAppAuthProviders[count, 5] == "Windows" && mySPWebApp.IisSettings[SPUrlZone.Extranet].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Extranet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 5] = WebAppAuthProviders[count, 5] + " (Kerberos + Basic)";

                            if (WebAppAuthProviders[count, 5] == "Windows" && !mySPWebApp.IisSettings[SPUrlZone.Extranet].UseWindowsIntegratedAuthentication && mySPWebApp.IisSettings[SPUrlZone.Extranet].UseBasicAuthentication)
                                WebAppAuthProviders[count, 5] = WebAppAuthProviders[count, 5] + " (Basic)";

                            if (mySPWebApp.IisSettings[SPUrlZone.Extranet].AllowAnonymous)
                                WebAppAuthProviders[count, 5] = WebAppAuthProviders[count, 5] + "| true";
                            else
                                WebAppAuthProviders[count, 5] = WebAppAuthProviders[count, 5] + "| false";

                            WebAppAuthProviders[count, 5] = WebAppAuthProviders[count, 5] + "| " + mySPWebApp.IisSettings[SPUrlZone.Extranet];

                            WebAppAuthProviders[count, 5] = WebAppAuthProviders[count, 5] + "| " + mySPWebApp.IisSettings[SPUrlZone.Extranet].UseClaimsAuthentication.ToString();
                        }

                        //finding out the content dbs for the web app
                        SPContentDatabaseCollection mySPContentDBCollection = mySPWebApp.ContentDatabases;
                        foreach (SPContentDatabase mySPContentDB in mySPContentDBCollection)
                        {
                            ContentDBcount--;
                            ContentDBs[count, ContentDBcount] = mySPContentDB.Name;
                            mySiteCollectionNum = mySiteCollectionNum + mySPContentDB.Sites.Count;
                            ContentDBSitesNum[count, ContentDBcount] = mySPContentDB.Sites.Count.ToString();
                        }
                        WebAppDetails[count, 4] = mySiteCollectionNum.ToString();

                        //enumerating alternateURLs
                        foreach (SPAlternateUrl mySPAlternateUrl in mySPWebApp.AlternateUrls)
                        {
                            switch (mySPAlternateUrl.UrlZone.ToString().Trim())
                            {
                                case "Default":
                                    if (WebAppPublicAAM[count, 1] == null)
                                        WebAppPublicAAM[count, 1] = mySPAlternateUrl.IncomingUrl.ToString();
                                    else
                                        WebAppInternalAAMURL[count, 1] = mySPAlternateUrl.IncomingUrl.ToString();
                                    break;
                                case "Intranet":
                                    if (WebAppPublicAAM[count, 2] == null)
                                        WebAppPublicAAM[count, 2] = mySPAlternateUrl.IncomingUrl.ToString();
                                    else
                                        WebAppInternalAAMURL[count, 2] = mySPAlternateUrl.IncomingUrl.ToString();
                                    break;
                                case "Internet":
                                    if (WebAppPublicAAM[count, 3] == null)
                                        WebAppPublicAAM[count, 3] = mySPAlternateUrl.IncomingUrl.ToString();
                                    else
                                        WebAppInternalAAMURL[count, 3] = mySPAlternateUrl.IncomingUrl.ToString();
                                    break;
                                case "Custom":
                                    if (WebAppPublicAAM[count, 4] == null)
                                        WebAppPublicAAM[count, 4] = mySPAlternateUrl.IncomingUrl.ToString();
                                    else
                                        WebAppInternalAAMURL[count, 4] = mySPAlternateUrl.IncomingUrl.ToString();
                                    break;
                                case "Extranet":
                                    if (WebAppPublicAAM[count, 5] == null)
                                        WebAppPublicAAM[count, 5] = mySPAlternateUrl.IncomingUrl.ToString();
                                    else
                                        WebAppInternalAAMURL[count, 5] = mySPAlternateUrl.IncomingUrl.ToString();
                                    break;
                            }
                        }
                        count++;
                    }
                }

                return true;
            }

            catch (Exception ex)
            {
                exceptionInformation = ex.Message;
                return false;
            }
        }

        public bool o14enumContentDBs()
        {
            double DiskSpaceReq = 0.000;
            ContentDBProps = new string[totalContentDBCount, 7];
            count = 0;
            string queryString;
            try
            {
                foreach (SPWebApplication webApplication in SPWebService.ContentService.WebApplications)
                {
                    SPContentDatabaseCollection contentDBs = webApplication.ContentDatabases;

                    foreach (SPContentDatabase contentDB in contentDBs)
                    {
                        ContentDBProps[count, 0] = contentDB.Name;
                        ContentDBProps[count, 1] = webApplication.Name;
                        ContentDBProps[count, 2] = contentDB.Id.ToString();
                        ContentDBProps[count, 3] = contentDB.ServiceInstance.DisplayName;
                        ContentDBProps[count, 4] = contentDB.Sites.Count.ToString();
                        DiskSpaceReq = (double)(contentDB.DiskSizeRequired / 1048576.000);
                        ContentDBProps[count, 5] = DiskSpaceReq.ToString() + " MB";
                        }
                }
                return true;
            }
            catch (Exception ex)
            {
                exceptionInformation = ex.Message;
                return false;
            }

        }

        public bool o14enumSolutions()
        {
            try
            {
                solutionCount = SPFarm.Local.Solutions.Count;
                solutionProps = new string[solutionCount, 6];
                count = 0;
                foreach (SPSolution solution in SPFarm.Local.Solutions)
                {
                    solutionProps[count, 0] = solution.DisplayName;
                    solutionProps[count, 1] = solution.Deployed.ToString();
                    solutionProps[count, 2] = solution.LastOperationDetails;
                    solutionProps[count, 5] = solution.Id.ToString();

                    foreach (SPServer deployedServer in solution.DeployedServers)
                    {
                        if (solutionProps[count, 3] == null)
                        {
                            if (deployedServer.Address == null)
                                solutionProps[count, 3] = "";
                            else
                                solutionProps[count, 3] = deployedServer.Address;
                        }
                        else
                            solutionProps[count, 3] = solutionProps[count, 3] + "<br>" + deployedServer.Address;
                    }
                    count = count + 1;
                }
                return true;
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-"))
                {
                    solutionProps[count, 4] = "It appears that this solution isn't deployed on any specific web app.";
                    return true;
                }

                exceptionInformation = ex.Message;
                return false;

            }

        }

        static void Main(string[] args)
        {

            Program prg = new Program();
            // call all functions in prg here
        }
    }
}
